package com.newhero.product.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

public class ExportUtil {
	
	@SuppressWarnings("rawtypes")
	public static void export(
			HttpServletRequest request,
			HttpServletResponse response, 
			List dataList, String fileName,String templateName, int numSheet, int startRow, int xStart, int xEnd){

		response.reset();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String dateStr = sdf.format(new Date());
        fileName += "-" + dateStr;
		try {
			fileName = new String(fileName.getBytes(),"ISO-8859-1");
		} catch (Exception e) {
			e.printStackTrace();
		}

        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\"");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
//        String basePrintPath = (String) request.getSession().getAttribute("basePrintPath");
        String basePrintPath = request.getRealPath("") + "/templates/excel/";
        String template = basePrintPath + templateName + ".xls";
        
        @SuppressWarnings("unchecked")
		HSSFWorkbook exportExcel = ExcelUtil.generateExcel(dataList, template, numSheet, startRow, xStart, xEnd);
		
        try {
			exportExcel.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
